using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CollectItem : MonoBehaviour
{
	private List<GameObject> pools;

	private WaitForSeconds waitForSeconds;

	[SerializeField]
	private int itemCount;

	[SerializeField]
	private float minRange;

	[SerializeField]
	private float maxRange;

	[SerializeField]
	private float delayTime;

	[SerializeField]
	private GameObject item;

	[SerializeField]
	private Transform sourcePoint;

	[SerializeField]
	private Transform targetPoint;

	[SerializeField]
	private Transform poolsParent;

	private void Start()
	{
		pools = new List<GameObject>();
		GameObject gameObject = null;
		for (int i = 0; i < itemCount; i++)
		{
			gameObject = UnityEngine.Object.Instantiate(item);
			gameObject.transform.parent = poolsParent;
			gameObject.SetActive(value: false);
			pools.Add(gameObject);
		}
		waitForSeconds = new WaitForSeconds(delayTime);
	}

	public void Collect(double cash)
	{
		double cashPerItem = Math.Round(cash / (double)itemCount);
		StartCoroutine(SpawnCash(cashPerItem));
	}

	private IEnumerator SpawnCash(double cashPerItem)
	{
		int i = itemCount;
		Vector3 position = Vector3.zero;
		while (i > 0)
		{
			yield return waitForSeconds;
			i--;
			float spawnRadius = UnityEngine.Random.Range(minRange, maxRange);
			position = UnityEngine.Random.insideUnitSphere * spawnRadius;
			position.z = 0f;
			position += sourcePoint.position;
			pools[i].SetActive(value: true);
			pools[i].transform.position = sourcePoint.position;
			pools[i].GetComponent<CashItem>().Initialize(cashPerItem, position, targetPoint);
		}
	}
}
